package demo01;

public class CalculatePrimes extends Thread{
	
	private static final int MAX_PRIMES =10000;
	private static final int TEN_SECONDS=1000;
	private volatile boolean finished = false;	//共享变量	
	
	
	public void run() {
		int[] primes = new int[MAX_PRIMES];
		int count = 0;
		for(int i=2;count<MAX_PRIMES;i++){
			if(finished){
				break;
			}
			boolean prime = true;
			for(int j=0;j<count;j++){
				if(i%primes[j]==0){
					prime = false;
					break;
				}
			}
			if(prime){
				primes[count++]=i;
				System.out.println("found time:"+i);
			}
		}
	}

	public static void main(String[] args) {
		CalculatePrimes cal = new CalculatePrimes();
		cal.start();
		try {
			Thread.sleep(TEN_SECONDS);//睡一秒
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cal.finished=true;
	}

}
